package com.thb.pay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.thb.pay.api.sys.vo.req.SysMessagePageReq;
import com.thb.pay.api.sys.vo.req.SysMessageReq;
import com.thb.pay.api.sys.vo.resp.SysMessageVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.system.entity.SysMessage;
import com.thb.pay.system.mapper.SysMessageMapper;
import com.thb.pay.system.service.SysMessageService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 系统-消息表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class SysMessageServiceImpl extends BaseServiceImpl<SysMessageMapper, SysMessage> implements SysMessageService {

    @Override
    public PageResult<SysMessageVo> findPage(SysMessagePageReq pageReq) {
        IPage<SysMessage> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<SysMessage> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, SysMessageVo.class);
    }

    @Override
    public SysMessageVo getById(Long id) {
        SysMessage sysMessage = super.selectById(id);
        if (sysMessage != null) {
            SysMessageVo sysMessageVo = new SysMessageVo();
            BeanUtil.copyProperties(sysMessage, sysMessageVo);
            return sysMessageVo;
        }
        return null;
    }

    @Override
    public void add(SysMessageReq sysMessageReq) {
        SysMessage sysMessage = new SysMessage();
        BeanUtil.copyProperties(sysMessageReq, sysMessage);
        super.insert(sysMessage);
    }

    @Override
    public void update(SysMessageReq sysMessageReq) {
        Assert.notNull(sysMessageReq, "sysMessageReq.not.null");
        Assert.notNull(sysMessageReq.getId(), "messageId.not.null");
        SysMessage sysMessage = super.selectById(sysMessageReq.getId());
        Assert.notNull(sysMessage, "message.not.null");
        SysMessage targetSysMessage = new SysMessage();
        BeanUtil.copyProperties(sysMessageReq, targetSysMessage);
        super.updateById(targetSysMessage);
    }

    @Override
    public void deleteById(Long id) {
        SysMessage sysMessage = super.selectById(id);
        if (sysMessage != null) {
            super.deleteById(id);
        }

    }

    @Override
    public void deleteBatchIds(List<Long> ids) {
        Assert.notEmpty(ids, "messageIds.not.null");
        super.deleteBatchIds(ids);
    }
}
